/*
 * Created: Author: Aléx Carvalho / Date: Feb 10, 2015
 * 
 * Changed: $Author: ac280593@gmail.com $ / $Date: 2015-06-04 21:24:45 -0300 (Thu, 04 Jun 2015) $
 */
package com.cpm.entities.id;

import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.MappedSuperclass;
import javax.persistence.TableGenerator;

@MappedSuperclass
public class IntegerId implements Id<Integer> {

    private static final long serialVersionUID = 1L;

    @TableGenerator(name = "sequence_generator", table = "sequences_ids", pkColumnName = "table_name",
            valueColumnName = "next_id", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "sequence_generator")
    @javax.persistence.Id
    protected Integer id;

    @Column(nullable = false)
    private Boolean active = true;

    public IntegerId() {
        id = null;
    }

    public IntegerId(Integer id) {
        this.id = id;
    }

    @Override
    public Integer getId() {
        return id;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }

    public Boolean isActive() {
        return active;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(id);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (obj instanceof IntegerId) {
            IntegerId other = (IntegerId) obj;

            if (id == null && other.id == null) {
                return this.equalsAllFields(obj);
            }

            return Objects.equals(id, ((IntegerId) obj).id);
        }

        return false;
    }

}
